#######################################
# Federal Democracy in the Laboratory #
# Rodolpho Bernabel                   #
# New York University                 #
# Last Update: JANUARY 19, 2015       #
#######################################

library(ggplot2)
library(Sleuth2)
library(plm)
library(plyr)

rm(list=ls(all=TRUE))
setwd("/Users/rodolphobernabel/Desktop/Power Decentralization and Democratic Incentives Against Corruption/Data/")
df.1 <- read.csv("AllCentralized.csv")
df.2 <- read.csv("AllFederal.csv")

################
# Prediction 1 #
################

percentageOfCorruptPresidentsReelectedInCD <- function(){
  replacement <- 0
  instance <- 0
  for(i in 1:1272){
    if(df.1$PlayerNum[i] == 2 & df.1$decision[i] == 0){
      replacement <- replacement + df.1$replace[i]
      instance <- instance + 1
    }
  }
  cat("reelection rate = ",(instance-replacement)/instance,"\n")
  cat("instances minus replacement = ", instance - replacement,"\n")
  cat("instances = ", instance,"\n")
  cat("replacement rate = ", replacement/instance)
}
percentageOfCorruptPresidentsReelectedInCD()

percentageOfCorruptPresidentsReelectedInFD <- function(){
  replacement <- 0
  instance <- 0
  for(i in 1:1533){
    if(df.2$PlayerNum[i] == 3 & df.2$decision[i] == 0){
      replacement <- replacement + df.2$replace[i]
      instance <- instance + 1
    }
  }
  cat("reelection rate = ",(instance-replacement)/instance,"\n")
  cat("instances minus replacement = ", instance - replacement,"\n")
  cat("instances = ", instance,"\n")
  cat("replacement rate = ", replacement/instance)
}
percentageOfCorruptPresidentsReelectedInFD()

#test
reelected <- c(85, 45)
corrupt <- c(127, 96)
prop.test(reelected,corrupt)

#footnote 7
percentageOfCorruptPresidentsReelectedInFDInPeriodsWithHonestGovernors <- function(){
  replacement <- 0                                     
  instance <- 0                                      
  for(i in 1:1533){                                      
    if(df.2$PlayerNum[i] == 1 & df.2$decision[i] == 0 & df.2$decisiong[i] == 1){
      replacement <- replacement + df.2$replace[i]
      instance <- instance + 1
    }
  }
  print((instance-replacement)/instance)
  print(instance - replacement)
  print (instance)
}
percentageOfCorruptPresidentsReelectedInFDInPeriodsWithHonestGovernors()

#########
# Logit #
#########

#data wrangling to merge df's
cntrlzd <- rep(1,1272)
df.1 <- cbind(df.1,cntrlzd)
cntrlzd <- rep(0,1533)
df.2 <- cbind(df.2,cntrlzd)
vars <- c("session", "context_free", "Period", "Subject", "president", "replace", "decision", "cntrlzd","PlayerNum")
df.1 <- df.1[-c(49:156),] #missing values
df.3 <- df.1[vars]
df.4 <- df.2[vars]
df.4 <- df.4[which(df.4$PlayerNum != 2),]#Exclude governors
df.3$PlayerNum <- as.numeric(df.3$PlayerNum)
df.3$PlayerNum <- df.3$PlayerNum - 1
df.5 <- rbind(df.3,df.4)
df.6 <- df.5[which(df.5$PlayerNum != 1),]#exclude presidents
df.7 <- df.6[which(df.6$decision == 0),]#corruption instances only

mod.1.1 <- glm(replace ~ cntrlzd, data = df.7, family = binomial)
summary(mod.1.1)

df.8 <- df.5[which(df.5$PlayerNum == 1 & df.5$president == 1),]#human presidents only

mod.2.1 <- glm(abs(1-decision) ~ cntrlzd, data = df.8, family = binomial)
summary(mod.2.1)

mod.2.2 <- glm(abs(1-decision) ~ cntrlzd + context_free, data = df.8, family = binomial)
summary(mod.2.2)

mod.2.3 <- glm(abs(1-decision) ~ cntrlzd + context_free + I(cntrlzd*context_free), data = df.8, family = binomial)
summary(mod.2.3)

#data wrangling to produce figure 1
meanCorruptReelectionPerPeriodInCD <- c()
for(i in 1:12){
  reelection <- 0
  instance <- 0
  for(j in 1:1164){
    if((df.1$Period[j] == i) && (df.1$decision[j] == 0)){
      reelection <- reelection + abs(1 - df.1$replace[j])
      instance <- instance + 1
    }
    meanCorruptReelectionPerPeriodInCD[i] <- reelection/instance
  }
}
meanCorruptReelectionPerPeriodInCD

meanCorruptReelectionPerPeriodInFD <- c()
for(i in 1:10){
  reelection <- 0
  instance <- 0
  for(j in 1:1533){
    if((df.2$Period[j] == i) && (df.2$decision[j] == 0)){
      reelection <- reelection + abs(1 - df.2$replace[j])
      instance <- instance + 1
    }
    meanCorruptReelectionPerPeriodInFD[i] <- reelection/instance
  }
}
meanCorruptReelectionPerPeriodInFD

per <- c(1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10) 
meanCorruptReelection <- c()
centr <- c()
for(i in 1:12){
  meanCorruptReelection[i] <- meanCorruptReelectionPerPeriodInCD[i]
  centr[i] <- "Centralized"
}
for(i in 13:22){
  meanCorruptReelection[i] <- meanCorruptReelectionPerPeriodInFD[i-12]
  centr[i] <- "Federal"
}
df.9 <- data.frame(per, meanCorruptReelection, centr)
Type <- centr
df.9 <- cbind(df.9,Type)

ggplot(df.9, aes(x = per, y = meanCorruptReelection, color = centr)) +
  stat_smooth(method = "lm", formula = y ~ x, size = 2, se = F) + geom_point()  +
  ggtitle("") + xlab("Period") + ylab("Proportion of Corrupt Presidents Reelected (Means)") +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +
  scale_colour_grey(name = "Type") +
  scale_x_continuous(breaks=1:12)

#######################
# Predictions 2 and 3 #
#######################

AvgCorruptionByNormalPresidentInCD <- function(){
  corruption <- 0
  normal <- 0
  for(i in 1:1164){
    if(df.1$PlayerNum[i] == 1 & df.1$president[i] == 1){
      corruption <- corruption + abs(1 - df.1$decision[i])
      normal <- normal + 1
    }
  }
  cat("rate = ",corruption/normal,"\n")
  cat("corruption = ",corruption,"\n")
  cat("normal = ",normal,"\n")
  cat("acted responsibly = ", 1-(corruption/normal))
}
AvgCorruptionByNormalPresidentInCD()

percentageOfHonestPresidentsReelectedInCD <- function(){
  replacement <- 0
  instance <- 0
  for(i in 1:1164){
    if(df.1$decision[i] == 1){
      replacement <- replacement + df.1$replace[i]
      instance <- instance + 1
    }
  }
  cat("reelection rate = ",(instance-replacement)/instance,"\n")
  cat("reelected = ",instance-replacement,"\n")
  cat("instances = ",instance)
}
percentageOfHonestPresidentsReelectedInCD()

################
# Prediction 4 #
################

percentageOfHonestPresidentsReelectedInFD <- function(){
  replacement <- 0
  instance <- 0
  for(i in 1:1533){
    if(df.2$decision[i] == 1){
      replacement <- replacement + df.2$replace[i]
      instance <- instance + 1
    }
  }
  cat("reelection rate = ",(instance-replacement)/instance,"\n")
  cat("reelected = ",instance-replacement,"\n")
  cat("instances = ",instance)
}
percentageOfHonestPresidentsReelectedInFD()

#data wranging to produce figure 2
meanCorruptionPerPeriodInCD <- c() #by normal presidents
for( i in 1:12){
  corruption <- 0
  instance <- 0
  for(j in 1:1164){
    if((df.1$Period[j] == i) && (df.1$PlayerNum[j] == 1)){
      corruption <- corruption + abs(1 - df.1$decision[j])
      instance <- instance + 1
    }
    meanCorruptionPerPeriodInCD[i] <- corruption/instance
  }
}
meanCorruptionPerPeriodInCD

meanCorruptionPerPeriodInFD <- c() #by normal presidents
for( i in 1:10){
  corruption <- 0
  instance <- 0
  for(j in 1:1533){
    if((df.2$Period[j] == i) && (df.2$PlayerNum[j] == 1)){#
      corruption <- corruption + abs(1 - df.2$decision[j])
      instance <- instance + 1
    }
    meanCorruptionPerPeriodInFD[i] <- corruption/instance
  }
}
meanCorruptionPerPeriodInFD

per <- c(1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10) 
meanCorruptPerPeriod <- c()
centr <- c()
for(i in 1:12){
  meanCorruptPerPeriod[i] <- meanCorruptionPerPeriodInCD[i]
  centr[i] <- "Centralized"
}
for(i in 13:22){
  meanCorruptPerPeriod[i] <- meanCorruptionPerPeriodInFD[i-12]
  centr[i] <- "Federal"
}
df.10 <- data.frame(per, meanCorruptPerPeriod, centr)
Type <- centr
df.10 <- cbind(df.10,Type)

ggplot(df.10, aes(x = per, y = meanCorruptPerPeriod, shape = Type, color = Type)) +
  stat_smooth(method = "lm", formula = y ~ x, size = 2, se = F) + geom_point()  +
  xlab("Period") + ylab("Corruption Rate (Normal Presidents)") +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +
  scale_colour_grey(name = "Type") +
  scale_x_continuous(breaks=1:12)

################
# Prediction 5 #
################

percentageOfCorruptPresidentsReelectedInFD <- function(){
  replacement <- 0
  instance <- 0
  for(i in 1:1533){
    if(df.2$PlayerNum[i] == 3 & df.2$decision[i] == 0){
      replacement <- replacement + df.2$replace[i]
      instance <- instance + 1
    }
  }
  cat("percentage replaced = ",(replacement)/instance,"\n")
  cat("replacements = ",replacement,"\n")
  cat("instances = ",instance)
}
percentageOfCorruptPresidentsReelectedInFD()

percentageOfCorruptGovernorsReelectedInFD <- function(){
  replacement <- 0
  instance <- 0
  for(i in 1:1533){
    if(df.2$PlayerNum[i] == 3 & df.2$decisiong[i] == 0){
      replacement <- replacement + df.2$replaceg[i]
      instance <- instance + 1
    }
  }
  cat("percentage replaced = ",(replacement)/instance,"\n")
  cat("replacements = ",replacement,"\n")
  cat("instances = ",instance)
}
percentageOfCorruptGovernorsReelectedInFD()

#test
replaced <- c(51, 82)
corrupt <- c(96, 181)
prop.test(replaced,corrupt)

################################
# Computers compared to Humans #
################################

percentageOfNormalCorruptPresidentsReelectedInCD <- function(){
  replacement <- 0
  instance <- 0
  for(i in 1:1164){
    if( (df.1$president[i] == 1) && (df.1$decision[i] == 0) && (df.1$PlayerNum[i] == 2) ){
      replacement <- replacement + df.1$replace[i]
      instance <- instance + 1
    }
  }
  cat("reelection rate = ",(instance-replacement)/instance, "\n")
  cat("reelected = ",instance-replacement, "\n")
  cat("instances = ",instance, "\n")
}
percentageOfNormalCorruptPresidentsReelectedInCD()

percentageOfComputerCorruptPresidentsReelectedInCD <- function(){
  replacement <- 0
  instance <- 0
  for(i in 1:1164){
    if( (df.1$president[i] == 3) && (df.1$decision[i] == 0) && (df.1$PlayerNum[i] == 2) ){
      replacement <- replacement + df.1$replace[i]
      instance <- instance + 1
    }
  }
  cat("reelection rate = ",(instance-replacement)/instance, "\n")
  cat("reelected = ",instance-replacement, "\n")
  cat("instances = ",instance, "\n")
}
percentageOfComputerCorruptPresidentsReelectedInCD()

#test
reelected <- c(27,1)
instance <- c(43,8)
prop.test(reelected,instance)

percentageOfNormalCorruptPresidentsReelectedInFD <- function(){
  replacement <- 0
  instance <- 0
  for(i in 1:1533){
    if( (df.2$president[i] == 1) && (df.2$decision[i] == 0) && (df.2$PlayerNum[i] == 1) ){
      replacement <- replacement + df.2$replace[i]
      instance <- instance + 1
    }
  }
  cat("reelection rate = ",(instance-replacement)/instance, "\n")
  cat("reelected = ",instance-replacement, "\n")
  cat("instances = ",instance, "\n")
}
percentageOfNormalCorruptPresidentsReelectedInFD()

percentageOfComputerCorruptPresidentsReelectedInFD <- function(){
  replacement <- 0
  instance <- 0
  for(i in 1:1533){
    if( (df.2$president[i] == 3) && (df.2$decision[i] == 0) && (df.2$PlayerNum[i] == 1) ){
      replacement <- replacement + df.2$replace[i]
      instance <- instance + 1
    }
  }
  cat("reelection rate = ",(instance-replacement)/instance, "\n")
  cat("reelected = ",instance-replacement, "\n")
  cat("instances = ",instance, "\n")
}
percentageOfComputerCorruptPresidentsReelectedInFD()

#test
reelected <- c(28,1)
instance <- c(61,9)
prop.test(reelected,instance)

#####################
# Language Analysis #
#####################

AvgCorruptionByNormalPresidentInCDInContextRich <- function(){
  corruption <- 0
  normal <- 0
  for(i in 1:1164){
    if((df.1$PlayerNum[i] == 1) && (df.1$president[i] == 1) & (df.1$context_free[i] == 0)){
      corruption <- corruption + abs(1 - df.1$decision[i])
      normal <- normal + 1
    }
  }
  cat("corruption rate = ",corruption/normal,"\n")
  cat("currupt = ",corruption,"\n")
  cat("instances = ",normal)
}
AvgCorruptionByNormalPresidentInCDInContextRich()

AvgCorruptionByNormalPresidentInCDInContextFree <- function(){
  corruption <- 0
  normal <- 0
  for(i in 1:1164){
    if((df.1$PlayerNum[i] == 1) && (df.1$president[i] == 1) & (df.1$context_free[i] == 1)){
      corruption <- corruption + abs(1 - df.1$decision[i])
      normal <- normal + 1
    }
  }
  cat("corruption rate = ",corruption/normal,"\n")
  cat("currupt = ",corruption,"\n")
  cat("instances = ",normal)
}
AvgCorruptionByNormalPresidentInCDInContextFree()

#test
corruption <- c(27, 60)
instance <- c(63, 157)
prop.test(corruption,instance)

AvgCorruptionByNormalPresidentInFDInContextRich <- function(){
  corruption <- 0
  normal <- 0
  for(i in 1:1533){
    if((df.2$PlayerNum[i] == 1) & (df.2$president[i] == 1) & (df.2$context_free[i] == 0)){
      corruption <- corruption + abs(1 - df.2$decision[i])
      normal <- normal + 1
    }
  }
  cat("corruption rate = ",corruption/normal,"\n")
  cat("currupt = ",corruption,"\n")
  cat("instances = ",normal)
}
AvgCorruptionByNormalPresidentInFDInContextRich()

AvgCorruptionByNormalPresidentInFDInContextFree <- function(){
  corruption <- 0
  normal <- 0
  for(i in 1:1533){
    if((df.2$PlayerNum[i] == 1) & (df.2$president[i] == 1) & (df.2$context_free[i] == 1)){
      corruption <- corruption + abs(1 - df.2$decision[i])
      normal <- normal + 1
    }
  }
  cat("corruption rate = ",corruption/normal,"\n")
  cat("currupt = ",corruption,"\n")
  cat("instances = ",normal)
}
AvgCorruptionByNormalPresidentInFDInContextFree()

corruption <- c(25, 36)
instance <- c(109, 133)
prop.test(corruption,instance)

#########################
#  Costlier Federalism  #
#########################

df.11 <- df.2[which(df.2$session==17),]

percentageOfCorruptPresidentsReelectedInCostlierFD <- function(){
  replacement <- 0
  instance <- 0
  for(i in 1:198){
    if(df.11$PlayerNum[i] == 3 & df.11$decision[i] == 0){
      replacement <- replacement + df.11$replace[i]
      instance <- instance + 1
    }
  }
  cat("reelection rate = ",(instance-replacement)/instance,"\n")
  cat("reelected = ",instance - replacement,"\n")
  cat("instances = ",instance)
}
percentageOfCorruptPresidentsReelectedInCostlierFD()

percentageOfCorruptPresidentsReelectedInFD <- function(){
  replacement <- 0
  instance <- 0
  for(i in 1:1533){
    if(df.2$PlayerNum[i] == 3 & df.2$decision[i] == 0){
      replacement <- replacement + df.2$replace[i]
      instance <- instance + 1
    }
  }
  cat("reelection rate = ",(instance-replacement)/instance,"\n")
  cat("reelected = ",instance - replacement,"\n")
  cat("instances = ",instance)
}
percentageOfCorruptPresidentsReelectedInFD()

#test
reelected <- c(4, 45)
corrupt <- c(9, 96)
prop.test(reelected,corrupt)

